/**
 * Created by Administrator on 2019/1/31.
 */
var table;
var parentCode;
$(function() {
    parentCode = locations.ParamString("parentCode");
    table = $("#noticeList");
    initComponent();
    initGrid();
    queryNoticeFunction();
});
function initComponent() {
    var curr_time = new Date();
    var strDate = curr_time.getFullYear() + "-";
    strDate += curr_time.getMonth() + 1 + "-";
    strDate += curr_time.getDate() + " 00:00:00";
    $("#startTime").datetimebox("setValue", strDate);
    $("#endTime").datetimebox("setValue", addDate(curr_time, 0) + " 23:59:59");
}
function initGrid(){
    var param = queryButtonLimit(parentCode);
    table.datagrid({
        iconCls : 'icon-ok',
        pageSize : 10,
        pageList : [ 10, 20, 40, 80 ],
        striped : true,
        collapsible : false,
        remoteSort : true,// 服务器端排序
        fitColumns : false,
        singleSelect : true,
        showFooter : true,
        checkOnSelect: false,
        selectOnCheck: false,
        columns : [[{
            field : 'id',
            checkbox : true,
            hidden:true
        }, {
            field : 'noticeNo',
            title : '公告编号',
            width : 150,
            align : 'center'
        }, {
            field : 'noticeTitle',
            title : '公告标题',
            width : 120,
            align : 'center'
        }, {
            field : 'noticeContent',
            title : '公告内容',
            width : 200,
            align : 'center'
        }, {
            field : 'isShow',
            title : '是否显示',
            checkbox : true,
            width : 100,
            align : 'center'
        }, {
            field : 'remark',
            title : '备注信息',
            width : 120,
            align : 'center'
        }, {
            field : 'entryNickname',
            title : '创建人',
            width : 120,
            align : 'center'
        }, {
            field : 'entryTime',
            title : '创建时间',
            width : 200,
            align : 'center',
            formatter:function (value, row, index) {
                if(row.entryTime == null) {
                    return "";
                }
                return dateFtt("yyyy-MM-dd hh:mm:ss",new Date(row.entryTime));
            }
        }
        ]],
        pagination : true,
        rownumbers : true,
        toolbar : param,
        onBeforeLoad : function(param) {
            var parameter = new Object();
            parameter.page = param.page;
            parameter.rows = param.rows;
            if($("#noticeNo").val()!="") parameter.noticeNo = $("#noticeNo").val();
            if($("#noticeTitle").val()!="") parameter.noticeTitle = $("#noticeTitle").val();
            if($("#remark").val()!="") parameter.remark = $("#remark").val();
            if($("#startTime").val()!="") parameter.startTime = $("#startTime").val();
            if($("#endTime").val()!="") parameter.endTime = $("#endTime").val();
            var jsonstr = postSyncAjax("/notice/selectPage", parameter);
            //console.log(jsonstr);
            //console.log(jsonstr.data);
            var json = jsonstr.data;
            var dataGridData = new Object();
            dataGridData.rows = json.rows;
            dataGridData.total = json.total;
            table.datagrid('loadData', dataGridData);
        },
        onLoadSuccess:function (data) {
            if(data) {
                $.each(data.rows, function(index, item){
                    if(item != null && item != undefined) {
                        if(item.isShow == 1){
                            table.datagrid('checkRow', index);
                        }
                        table.parent().find("input[name='isShow']").eq(index).click(function () {
                            changeDisplay($(this), item.noticeNo, item.isShow);
                        });
                    }
                });
                //console.log(table.parent().find("div .datagrid-cell-check>input[name='isShow']"));
            }
            //<td field="isShow"><div style="" class="datagrid-cell-check"><input type="checkbox" name="isShow" value=""></div></td>
            //console.log(table.parent().html());
            // 修改表头的默认的复选框、修改复选框列的默认宽度
            table.parent().find("div .datagrid-header-check").attr("style","width:100px;").html("是否显示");
            table.parent().find("div .datagrid-cell-check").attr("style","width:100px;");
        }
    });
}
//删除
var deleteNotice = function(uri){
    var rows = table.datagrid('getSelected');
    if (null == rows) {
        $.messager.alert("消息提示", "至少选择一条记录", "warning");
        return;
    } else {
        $.messager.confirm("确认对话框","确定要删除该公告信息？",function (r) {
            if(r){
                var parameter = new Object();
                parameter.id = rows.id;
                //console.log(rows.id);
                var jsonstr = postSyncAjax(uri, parameter);
                //console.log(jsonstr);
                if(jsonstr.success) {
                    //$.messager.alert("消息提示", "删除成功！", "warning");
                    showDialogWithFunc("公告删除成功！",function () {
                        table.datagrid("reload");
                    });
                } else {
                    //$.messager.alert("消息提示", "模具删除成功！", "warning");
                    showDialogWithContent("公告删除失败："+jsonstr.msg);
                }
            }
        });
    }
}
// 查询和重置
var queryNoticeFunction = function () {
    $("#queryNoticeBtn").click(function () {
        table.datagrid("reload");
    });
    $("#restoreBtn").click(function () {
        $("form[name='queryForm']").form('reset');
        initComponent();
        table.datagrid("reload");
    });
}
// 新增
var addNotice = function (uri) {
    //top.$('#cur_id').val(rows.id);
    //console.log("ceshi:"+rows.id);
    showThePage(parentCode, uri);

    //openMessageZone(uri);
    //createTabs('sh-tabs','/notice/add','新增公告','menu_notice_add');
}
// 显示或者取消显示公告
var changeDisplay = function (item, noticeNo, isShow) {
    var msg = ""; var tip = ""; var s;
    if(isShow == 1) {
        msg = "是否取消显示该公告？"; s = true;
        tip = "公告取消显示";
    } else {
        msg = "是否显示该公告？"; s = false;
        tip = "公告设置显示";
    }
    item.prop("checked", s);
    $.messager.confirm("确认对话框", msg, function (r) {
        if (r) {
            var parameter = new Object();
            parameter.noticeNo = noticeNo;
            var jsonstr = postSyncAjax("/notice/changeDisplay", parameter);
            //console.log(jsonstr);
            if(jsonstr.success) {
                showDialogWithFunc(tip + "成功！",function () {
                    item.prop("checked", !s);
                    table.datagrid("reload");
                });
            } else {
                showDialogWithContent(tip + "失败："+jsonstr.msg);
                item.prop("checked", s);
            }
        } else {
            item.prop("checked", s);
        }
    });
}